/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package res.elements;


import java.awt.Point;
import java.util.ArrayList;

import plar.core.Action;
import plar.core.Common;
import plar.core.Element;
import plar.core.Parameters;
import plar.core.Sprite;


import plar.core.Player;

import java.awt.Rectangle;

import org.jbox2d.collision.AABB;
import org.jbox2d.collision.Shape;


public class Mine extends Flash {
	
	public Mine()
	{
		//setProperty("lifeTime",200);
		//setProperty("explosionRelativeRectangle",new Rectangle(-150,-150,300,300));
		//setProperty("explosionDamage",40);
		
		actions.addAction("mine",new Action(){
				int timer =0;
				
				public Parameters call(Parameters parameters)
				{
					Element me = (Element) parameters.get("me");
					String subAction = (String) parameters.get("subAction");
					if (subAction == "main") {
					timer++;
					
					if(timer>=(Integer)me.getProperty("explosionTime")
					&& timer<=(Integer)me.getProperty("lifeTime")) {
						Common.info(8,"mine exploded!"+timer+"/"+((Integer)me.getProperty("explosionTime")));
						AABB rr =((AABB) me.getProperty("explosionRelativeRectangle"));
						AABB ar =new AABB();
						ar.lowerBound=me.body.getWorldPoint(rr.lowerBound);
						ar.upperBound=me.body.getWorldPoint(rr.upperBound);
						
						Shape [] ss=me.level.world.query(ar, me.level.world.getBodyCount());
						Common.info(8,"relative rectnagle"+ar+" shape size:"+ss.length);
						for(Shape s: ss)
						{
							Element p=(Element) s.getBody().getUserData();
							
						if(((String)p.getProperty("type")).equals("player"))
						{
								Integer energy = (Integer) p.getProperty("energy");
								p.setProperty("energy", energy - ((Integer)me.getProperty("damage")));
								Common.info(5,"shooted to:" + p);
						}
							}
						
						
			}
		}
			return null;
		}
			
		});
	}
}
	
